﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using ebay.Dal.CenterDB_EbayTableAdapters;
using System.Data;
using ebay.Utility;
using System.Drawing;

namespace ebay.Dal
{
    public class EbayActiveListing
    {
        //public bool Add(string ebayUserId, List<ebay.Model.EbayActiveListing> listEbayActiveListing)
        //{
        //    string cmdText1 = "Select 1 From EbayActiveListing Where ItemId = '{0}' And EbayUserId = '{1}'";
        //    using (SqlConnection connection = new SqlConnection(SqlHelper.ceterDB_Conn))
        //    {
        //        connection.Open(); 
        //        T_EbayActiveListingTableAdapter ebaySellingAdapter = new T_EbayActiveListingTableAdapter();
        //        ebaySellingAdapter.Connection = connection;
        //        using (SqlTransaction trans = connection.BeginTransaction())
        //        {
        //            ebaySellingAdapter.Transaction = trans;
        //            foreach (ebay.Model.EbayActiveListing eListing in listEbayActiveListing)
        //            {
        //                try
        //                {
        //                    //查询本条订单在数据库是否存在,如果存在则会返回改订单的状态
        //                    object val = SqlHelper.ExecuteScalar(trans, CommandType.Text,
        //                        string.Format(cmdText1, eListing.ItemId, eListing.EbayUserId,eListing.DownloadDate),
        //                        null);

        //                    if (val != null && val != DBNull.Value)
        //                    {
        //                        ebaySellingAdapter.Delete(eListing.EbayUserId, eListing.ItemId);
        //                    }
        //                    //新增订单记录
        //                    ebaySellingAdapter.Insert(eListing.EbayUserId, eListing.ItemId, eListing.DownloadDate, eListing.CustomLable, eListing.ProductIDType,
        //                        eListing.ProductIDValue, eListing.ProductIDValue2, eListing.QuantityAvailable, eListing.Purchases, eListing.Bids,
        //                        (decimal)eListing.Price, eListing.StartDate, eListing.EndDate, eListing.Condition, eListing.Type,
        //                        eListing.ItemTitle, eListing.CategoryLeafName, eListing.CategoryNumber, eListing.PrivateNotes,
        //                        eListing.SiteListed, eListing.VariationDetails, eListing.ProductReferenceID, eListing.ConditionID);
        //                }
        //                catch (Exception ex)
        //                {
        //                    SyncLog.CreateLog(LogAction.ActiveListing, ebayUserId, "写入数据库失败,详细原因请参阅错误日志 ...", Color.Red);
        //                    SyncLog.CreateErrDetailLog(ebayUserId, "ActiveListing Insert失败 ...");
        //                    SyncLog.CreateErrDetailLog(ebayUserId, "原因：" + ex.Message);
        //                    SyncLog.CreateErrDetailLog(ebayUserId, "错误实体：");
        //                    SyncLog.CreateErrDetailLog(ebayUserId, XmlHelper.XmlSerializer<ebay.Model.EbayActiveListing>(eListing));
        //                    return false;
        //                }
        //            }
                  
        //            try
        //            {
        //                trans.Commit();
        //                return true;
        //            }
        //            catch (Exception ex)
        //            {
        //                SyncLog.CreateLog(LogAction.ActiveListing, ebayUserId, "事物提交失败,详细原因请参阅日志文件 ...", Color.Red);
        //                SyncLog.CreateErrDetailLog(ebayUserId, "ActiveListing 事务提交失败 ...");
        //                SyncLog.CreateErrDetailLog(ebayUserId, "原因：" + ex.Message);
        //                trans.Rollback();
        //            }
        //            return true;
        //        }
        //    }

        //}

        public void DeleteByEbayUserId(string ebayUserId)
        {
            T_EbayActiveListingTableAdapter ebaySellingAdapter = new T_EbayActiveListingTableAdapter();
            ebaySellingAdapter.DeleteByEbayUserId(ebayUserId);
        }

        public System.Data.DataTable GetItemsByProductId(string productId)
        {
            string cmdText = @" Select o.EbayUserId,o.ItemId,a.Token,o.Price,
	        o.CustomLable,c.Listing,p.UserCode,p.ProductId,p.FitemId,
	        case when len(p2.ProductId2) > 0 then 1 else 0 end as pt
            From EbayActiveListing o 
            Left Outer Join CustomLableMapping c on c.CustomerId = dbo.ReplaceCustomLable(o.EbayUserId,o.CustomLable)
            Left Outer Join Product p on Charindex
							            (
								            ']' + p.UserCode + '[',
								            ']' + c.Listing
							            ) > 0
            Left Outer Join Product2 p2 on p.ProductId = p2.ProductId
            Left Outer Join EbayAccount a on o.EbayUserId = a.EbayUserId
            where p.ProductId = @ProductId";

            return SqlHelper.ExecuteDataTable(SqlHelper.ceterDB_Conn, CommandType.Text, cmdText, new SqlParameter("@ProductId", productId));
        }
    }
}
